package com.zycfc.zsf.boot.core.web.data;

import java.io.*;
import io.swagger.annotations.*;
import com.fasterxml.jackson.annotation.*;
import com.zycfc.zsf.boot.exception.*;
import org.slf4j.*;

@ApiModel(value = "DataResponse", description = "\u8fd4\u56de\u7684\u636e\u7c7b\u578b")
public class DataResponse<T> implements Serializable
{
    private static final Logger log;
    private static final long serialVersionUID = 1L;
    public static final String SUCCESS = "0000000";
    private static final String SUCCESS_MSG = "\u64cd\u4f5c\u6210\u529f";
    public static final String ERROR = "0000001";
    private static final String ERROR_MSG = "\u64cd\u4f5c\u5931\u8d25";
    private static final String PARAM_PRE = "{";
    private static final String PARAM_SUF = "}";
    @ApiModelProperty("\u8fd4\u56de\u7801")
    private String code;
    @ApiModelProperty("\u8fd4\u56de\u6d88\u606f")
    private String message;
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @ApiModelProperty("\u8fd4\u56de\u4e1a\u52a1\u5bf9\u8c61")
    private T data;
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @ApiModelProperty("\u5206\u9875\u4fe1\u606f")
    private Page pagination;
    
    public DataResponse() {
    }
    
    public DataResponse(final Builder builder) {
        this.code = builder.code;
        this.message = builder.message;
    }
    
    public DataResponse(final T object) {
        this.code = "0000000";
        this.message = "\u64cd\u4f5c\u6210\u529f";
        this.data = object;
    }
    
    public DataResponse(final T object, final int current, final int pageSize, final long total, final long pages) {
        this(object);
        this.pagination = new Page(current, pageSize, total, pages);
    }
    
    public static DataResponse succeed() {
        return builder().code("0000000").message("\u64cd\u4f5c\u6210\u529f").build();
    }
    
    public static <T> DataResponse<T> succeed(final T data) {
        final DataResponse<T> defaultResponse = (DataResponse<T>)succeed();
        defaultResponse.setData(data);
        return defaultResponse;
    }
    
    public static <T> DataResponse<T> succeed(final T data, final int current, final int pageSize, final long total, final long pages) {
        final DataResponse<T> defaultResponse = (DataResponse<T>)succeed();
        defaultResponse.setData(data);
        final Page pagination = new Page(current, pageSize, total, pages);
        defaultResponse.setPagination(pagination);
        return defaultResponse;
    }
    
    public static <T> DataResponse<T> failed() {
        return (DataResponse<T>)builder().code("0000001").message("\u64cd\u4f5c\u5931\u8d25").build();
    }
    
    public static <T> DataResponse<T> failed(final String code, final String msg, final T data) {
        final DataResponse<T> response = (DataResponse<T>)builder().code(code).message(msg).build();
        response.setData(data);
        return response;
    }
    
    public static <T> DataResponse<T> failed(final String code, final String msg) {
        return failed(code, msg, (T)null);
    }
    
    public static <T> DataResponse<T> failed(final IExceptionCode abstractEnum) {
        return failed((String)abstractEnum.getValue(), abstractEnum.getMessage(), (T)null);
    }
    
    public static <T> DataResponse<T> failed(final IExceptionCode abstractEnum, final String... params) {
        return failed((String)abstractEnum.getValue(), assembleRetMsg(abstractEnum, params), (T)null);
    }
    
    public static <T> DataResponse<T> failed(final IExceptionCode abstractEnum, final T data) {
        return failed((String)abstractEnum.getValue(), abstractEnum.getMessage(), data);
    }
    
    public static <T> DataResponse<T> failed(final IExceptionCode abstractEnum, final String[] paramArr, final T data) {
        return failed((String)abstractEnum.getValue(), assembleMsg(abstractEnum, paramArr), data);
    }
    
    public static <T> DataResponse<T> failed(final IExceptionCode abstractEnum, final T data, final String... params) {
        return failed((String)abstractEnum.getValue(), assembleRetMsg(abstractEnum, params), data);
    }
    
    public static Builder builder() {
        return new Builder();
    }
    
    private static String assembleMsg(final IExceptionCode code, final String[] paramArr) {
        String msg = code.getMessage();
        if (msg == null) {
            return msg;
        }
        if (!msg.contains("{") || !msg.contains("}")) {
            return msg;
        }
        final String[] msgArr = msg.split("}");
        int fromIndex = 0;
        Integer paramIndex = null;
        Integer paramKey = null;
        String paramStr = null;
        final int maxIndex = paramArr.length - 1;
        for (int paramCount = msg.endsWith("}") ? msgArr.length : (msgArr.length - 1), i = 0; i < paramCount; ++i) {
            paramIndex = msg.indexOf("{", fromIndex) + 1;
            paramKey = Integer.parseInt(msg.substring(paramIndex, paramIndex + 1));
            if (paramKey > maxIndex) {
                DataResponse.log.error("\u66ff\u6362\u54cd\u5e94\u63cf\u8ff0\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\uff0c\u66ff\u6362\u7d22\u5f15:{}\uff0c\u6570\u7ec4\u6700\u5927\u7d22\u5f15:{}\uff0c\u54cd\u5e94\u63cf\u8ff0:{}", new Object[] { paramKey, maxIndex, msg });
                return msg;
            }
            paramStr = "{" + paramKey + "}";
            if (msg.contains(paramStr)) {
                msg = msg.replace(paramStr, paramArr[paramKey]);
            }
            fromIndex = paramIndex + 2;
        }
        return msg;
    }
    
    private static String assembleRetMsg(final IExceptionCode code, final String... params) {
        String msg = code.getMessage();
        if (msg == null) {
            return msg;
        }
        if (!msg.contains("{") || !msg.contains("}")) {
            return msg;
        }
        final String[] msgArr = msg.split("}");
        int fromIndex = 0;
        Integer paramIndex = null;
        final int maxIndex = params.length - 1;
        for (int paramCount = msg.endsWith("}") ? msgArr.length : (msgArr.length - 1), i = 0; i < paramCount; ++i) {
            paramIndex = msg.indexOf("{", fromIndex);
            if (i > maxIndex) {
                DataResponse.log.error("\u66ff\u6362\u54cd\u5e94\u63cf\u8ff0\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\uff0c\u66ff\u6362\u7d22\u5f15:{}\uff0c\u5165\u53c2\u6570\u7ec4\u6700\u5927\u7d22\u5f15:{}\uff0c\u54cd\u5e94\u63cf\u8ff0:{}", new Object[] { i, maxIndex, msg });
                return msg;
            }
            if (msg.length() - paramIndex > 2) {
                msg = msg.substring(0, paramIndex) + params[i] + msg.substring(paramIndex + 2);
            }
            else {
                msg = msg.substring(0, paramIndex) + params[i];
            }
            fromIndex = paramIndex + params[i].length() - 1;
        }
        return msg;
    }
    
    public String getCode() {
        return this.code;
    }
    
    public String getMessage() {
        return this.message;
    }
    
    public T getData() {
        return this.data;
    }
    
    public Page getPagination() {
        return this.pagination;
    }
    
    public void setCode(final String code) {
        this.code = code;
    }
    
    public void setMessage(final String message) {
        this.message = message;
    }
    
    public void setData(final T data) {
        this.data = data;
    }
    
    public void setPagination(final Page pagination) {
        this.pagination = pagination;
    }
    
    @Override
    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof DataResponse)) {
            return false;
        }
        final DataResponse<?> other = (DataResponse<?>)o;
        if (!other.canEqual(this)) {
            return false;
        }
        final Object this$code = this.getCode();
        final Object other$code = other.getCode();
        Label_0065: {
            if (this$code == null) {
                if (other$code == null) {
                    break Label_0065;
                }
            }
            else if (this$code.equals(other$code)) {
                break Label_0065;
            }
            return false;
        }
        final Object this$message = this.getMessage();
        final Object other$message = other.getMessage();
        Label_0102: {
            if (this$message == null) {
                if (other$message == null) {
                    break Label_0102;
                }
            }
            else if (this$message.equals(other$message)) {
                break Label_0102;
            }
            return false;
        }
        final Object this$data = this.getData();
        final Object other$data = other.getData();
        Label_0139: {
            if (this$data == null) {
                if (other$data == null) {
                    break Label_0139;
                }
            }
            else if (this$data.equals(other$data)) {
                break Label_0139;
            }
            return false;
        }
        final Object this$pagination = this.getPagination();
        final Object other$pagination = other.getPagination();
        if (this$pagination == null) {
            if (other$pagination == null) {
                return true;
            }
        }
        else if (this$pagination.equals(other$pagination)) {
            return true;
        }
        return false;
    }
    
    protected boolean canEqual(final Object other) {
        return other instanceof DataResponse;
    }
    
    @Override
    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final Object $code = this.getCode();
        result = result * 59 + (($code == null) ? 43 : $code.hashCode());
        final Object $message = this.getMessage();
        result = result * 59 + (($message == null) ? 43 : $message.hashCode());
        final Object $data = this.getData();
        result = result * 59 + (($data == null) ? 43 : $data.hashCode());
        final Object $pagination = this.getPagination();
        result = result * 59 + (($pagination == null) ? 43 : $pagination.hashCode());
        return result;
    }
    
    @Override
    public String toString() {
        return "DataResponse(code=" + this.getCode() + ", message=" + this.getMessage() + ", data=" + this.getData() + ", pagination=" + this.getPagination() + ")";
    }
    
    static {
        log = LoggerFactory.getLogger((Class)DataResponse.class);
    }
    
    public static class Builder
    {
        private String code;
        private String message;
        
        Builder() {
        }
        
        public DataResponse build() {
            return new DataResponse(this);
        }
        
        public String getCode() {
            return this.code;
        }
        
        public Builder code(final String code) {
            this.code = code;
            return this;
        }
        
        public String getMessage() {
            return this.message;
        }
        
        public Builder message(final String message) {
            this.message = message;
            return this;
        }
    }
}
